package com.kaifamiao.wendao.servlet;

import com.kaifamiao.utils.StringHelper;
import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.service.CustomerService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet( "/customer/sign/up" )
public class CustomerSignUpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {
        String path = "/WEB-INF/pages/customer/sign-up.jsp" ;
        RequestDispatcher dispatcher = request.getRequestDispatcher( path );
        dispatcher.forward( request , response );
    }

    @Override
    protected void doPost( HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {

        HttpSession session = request.getSession();

        if( this.validate( request ) ) {
            // 获取请求参数
            String username = request.getParameter( "username" );
            String nickname = request.getParameter( "nickname" );
            String password = request.getParameter( "password" );
            // 获得注册时的IP地址
            String ip = request.getRemoteAddr();
            // 根据接受到的数据创建一个 Customer 实例
            Customer c = new Customer( username , password , nickname , ip );
            CustomerService cs = new CustomerService();
            // 调用 CustomerService中的 register方法继续处理 Customer 实例
            boolean result = cs.register( c );
            if( result ) {
                session.setAttribute( "message" , "注册成功" );
                response.sendRedirect(request.getContextPath() + "/customer/sign/in");
                return;
            }
            session.setAttribute( "message" , "注册失败" );
        }

        response.sendRedirect(  request.getContextPath() + "/customer/sign/up" );
    }

    private boolean validate( HttpServletRequest request ){
        HttpSession session = request.getSession();

        String nickname = request.getParameter( "nickname" );
        // username == null || username.trim().isEmpty()
        if( StringHelper.empty( nickname ) ){
            session.setAttribute( "message" , "你不填个昵称吗?" );
            return false ;
        }

        String username = request.getParameter( "username" );
        // username == null || username.trim().isEmpty()
        if( StringHelper.empty( username ) ){
            session.setAttribute( "nickname" , nickname );
            session.setAttribute( "message" , "用户名不可以为空" );
            return false ;
        }

        String password = request.getParameter( "password" );
        if( StringHelper.empty( password ) ){
            session.setAttribute( "nickname" , nickname );
            session.setAttribute( "username" , username );
            session.setAttribute( "message" , "密码不可以为空" );
            return false ;
        }

        String confirm = request.getParameter( "confirm" );
        if( !password.equals( confirm ) ){
            session.setAttribute( "nickname" , nickname );
            session.setAttribute( "username" , username );
            session.setAttribute( "message" , "两次输入的密码不一致" );
            return false ;
        }

        // 获取用户输入的验证码字符串
        String captcha = request.getParameter( "captcha" );
        if( StringHelper.empty( captcha ) ){
            session.setAttribute( "nickname" , nickname );
            session.setAttribute( "username" , username );
            session.setAttribute( "message" , "验证码不能为空" );
            return false ;
        }
        // 因为在 CaptchaImageServlet 中通过 session.setAttribute( "CAPTCHA" , code ) 设置属性
        // 所以通过属性名 "CAPTCHA" 可以获得保存在会话对象中的 验证码字符串
        String code = (String)session.getAttribute( "CAPTCHA" );
        // 比较会话中保存的验证码字符串 是否【不等于】 用户输入的验证码字符串
        if( !code.equalsIgnoreCase( captcha ) ){
            session.setAttribute( "nickname" , nickname );
            session.setAttribute( "username" , username );
            session.setAttribute( "message" , "你输入的验证码是错误的" );
            return false ;
        }

        return true ;
    }

}
